#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
简单的User-Agent生成器
替代fake_useragent，避免打包问题
"""

import random

class SimpleUserAgent:
    """简单的User-Agent生成器"""
    
    def __init__(self):
        # 常用的User-Agent列表
        self.user_agents = [
            # Chrome
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36',
            
            # Firefox
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0',
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:119.0) Gecko/20100101 Firefox/119.0',
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:118.0) Gecko/20100101 Firefox/118.0',
            
            # Edge
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0',
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0',
            
            # Safari
            'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
            'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15',
        ]
    
    @property
    def chrome(self):
        """获取Chrome User-Agent"""
        chrome_agents = [ua for ua in self.user_agents if 'Chrome' in ua and 'Edg' not in ua and 'Firefox' not in ua]
        return random.choice(chrome_agents)
    
    @property
    def firefox(self):
        """获取Firefox User-Agent"""
        firefox_agents = [ua for ua in self.user_agents if 'Firefox' in ua]
        return random.choice(firefox_agents)
    
    @property
    def edge(self):
        """获取Edge User-Agent"""
        edge_agents = [ua for ua in self.user_agents if 'Edg' in ua]
        return random.choice(edge_agents)
    
    @property
    def safari(self):
        """获取Safari User-Agent"""
        safari_agents = [ua for ua in self.user_agents if 'Safari' in ua and 'Chrome' not in ua]
        return random.choice(safari_agents)
    
    @property
    def random(self):
        """获取随机User-Agent"""
        return random.choice(self.user_agents)
    
    def __getitem__(self, browser):
        """支持索引访问"""
        if browser.lower() == 'chrome':
            return self.chrome
        elif browser.lower() == 'firefox':
            return self.firefox
        elif browser.lower() == 'edge':
            return self.edge
        elif browser.lower() == 'safari':
            return self.safari
        else:
            return self.random

# 创建全局实例
simple_ua = SimpleUserAgent()

def get_user_agent(browser='random'):
    """获取User-Agent的便捷函数"""
    if browser.lower() == 'chrome':
        return simple_ua.chrome
    elif browser.lower() == 'firefox':
        return simple_ua.firefox
    elif browser.lower() == 'edge':
        return simple_ua.edge
    elif browser.lower() == 'safari':
        return simple_ua.safari
    else:
        return simple_ua.random

# 兼容fake_useragent的接口
class UserAgent:
    """兼容fake_useragent.UserAgent的接口"""
    
    def __init__(self, *args, **kwargs):
        self.ua = SimpleUserAgent()
    
    @property
    def chrome(self):
        return self.ua.chrome
    
    @property
    def firefox(self):
        return self.ua.firefox
    
    @property
    def edge(self):
        return self.ua.edge
    
    @property
    def safari(self):
        return self.ua.safari
    
    @property
    def random(self):
        return self.ua.random
    
    def __getitem__(self, browser):
        return self.ua[browser]

# 用于替换fake_useragent的导入
def patch_fake_useragent():
    """修补fake_useragent导入"""
    import sys
    
    # 创建模拟的fake_useragent模块
    class FakeUserAgentModule:
        UserAgent = UserAgent
        
        def __getattr__(self, name):
            if name == 'UserAgent':
                return UserAgent
            return getattr(simple_ua, name, simple_ua.random)
    
    # 替换sys.modules中的fake_useragent
    sys.modules['fake_useragent'] = FakeUserAgentModule()
    print("✅ 已使用简单User-Agent替换fake_useragent")

if __name__ == "__main__":
    # 测试
    ua = UserAgent()
    print("Chrome:", ua.chrome)
    print("Firefox:", ua.firefox)
    print("Random:", ua.random)
